package com.msmall.community.play.cases.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.community.play.cases.dao.ScenarioDao;
import com.msmall.community.play.cases.model.Action;
import com.msmall.community.play.cases.model.Cases;
import com.msmall.community.play.cases.model.Scenario;
import com.msmall.community.play.cases.model.Target;
import com.msmall.community.play.cases.service.ScenarioManager;
import com.msmall.community.user.service.UserManager;

public class ScenarioManagerImpl extends BaseManagerImpl<Scenario, Long> implements ScenarioManager{

	private static final Log log = LogFactory.getLog(ScenarioManagerImpl.class);
	
	private ScenarioDao scenarioDao;

	private UserManager userManager;

	public void setScenarioDao(ScenarioDao scenarioDao) {
		this.scenarioDao = scenarioDao;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public void getScenariosByCases(PaginationSupport page, Long casesId){
		String queryString = "from Scenario s where s.cases.casesId="+casesId;
		page.setQueryString(queryString);
		getPagination(page);
	}
	
	public void saveScenario(Scenario scenario, Long casesId, Long actionId, Long targetId){
		Cases cases = (Cases) scenarioDao.get(Cases.class, casesId);
		scenario.setCases(cases);
		scenario.setCreateTime(new Date());
		String scenarioCode = "";
		if(actionId == null){
			actionId = 0L;
		}
		if(targetId == null){
			targetId = 0L;
		}
		scenarioCode = actionId + "-" + targetId;
		scenario.setScenarioCode(scenarioCode);
		scenarioDao.save(scenario);
	}
	
	public void removeScenario(Long scenarioId) throws Exception{
		try{
			scenarioDao.removeScenario(scenarioId);
		}catch(Exception e){
			log.error(e);
			throw e;
		}
	}
	
	public void saveAction(Action action, Long scenarioId){
		action.setCreateTime(new Date());
		action.setScenario((Scenario) scenarioDao.get(Scenario.class, scenarioId));
		scenarioDao.saveObject(action);
	}
	
	public void saveTarget(Target target, Long scenarioId){
		target.setScenario((Scenario) scenarioDao.get(Scenario.class, scenarioId));
		scenarioDao.saveObject(target);
	}
	
	/**
	 * 删除动作时，同时删除相关的场景，通过scenarioCode查找相关的场景.需要级联
	 * @param actionId
	 * @throws Exception 
	 */
	public void removeAction(Long actionId) throws Exception{
		List<Scenario> children = scenarioDao.getByAction(actionId);
		if(children != null){
			for(int i=0; i<children.size(); i++){
				Scenario scenario = children.get(i);
				removeScenario(scenario.getScenarioId());
			}
			
		}
		scenarioDao.remove(Action.class, actionId);
	}
	/**
	 * 删除对象时，同时删除相关的场景，通过scenarioCode查找相关的场景.需要级联
	 * @param targetId
	 * @throws Exception 
	 */
	public void removeTarget(Long targetId) throws Exception{
		List<Scenario> children = scenarioDao.getByTarget(targetId);
		if(children != null){
			for(int i=0; i<children.size(); i++){
				Scenario scenario = children.get(i);
				removeScenario(scenario.getScenarioId());
			}
			
		}
		scenarioDao.remove(Target.class, targetId);
	}
	
	/**
	 * 通过场景ID获得此场景下所有的ACTION
	 * @param scenarioId
	 * @return
	 * @throws Exception 
	 */
	public List<Action> getActionsByScenario(Long scenarioId) throws Exception{
		return scenarioDao.getActionsByScenario(scenarioId);
	}
	
	/**
	 *  通过场景ID获得此场景下所有的TARGET
	 * @param scenarioId
	 * @return
	 * @throws Exception 
	 */
	public List<Target> getTargetsByScenario(Long scenarioId) throws Exception{
		return scenarioDao.getTargetsByScenario(scenarioId);
	}
}
